from globalvals import *

# Parent class for all the different types of ship in the game
class Ship(object):
    def __init__(self, pos, width, height, speed, rate):
        self.pos = pos
        self.width = width
        self.height = height
        self.speed = speed   # speed is given in pixels per cycle
        self.rate = rate # rate is the wait time between shots in cycles

    def move(self, direction):
        if (direction == 'left'):
        	self.setPos([self.getPos()[0] - self.speed, self.getPos()[1]])
        	# Ensures the left edge does not pass the left border of the screen
        	if (self.getPos()[0] < 0):
        		self.setPos([0, self.getPos()[1]])

        elif (direction == 'right'):
        	self.setPos([self.getPos()[0] + self.speed, self.getPos()[1]])
        	# Ensures the right edge does not pass the right border of the screen
            #print (type(self.getPos()))
        	if (self.getPos()[0] + self.getWidth() > SCREEN_WIDTH):
        		self.setPos([SCREEN_WIDTH - self.getWidth(), self.getPos()[1]])

        elif (direction == 'up'):
        	self.setPos([self.getPos()[0], self.getPos()[1] - self.speed])
        	# Ensures the top edge does not pass above the top of the screen
        	if (self.getPos()[1] < 0):
        		self.setPos([self.getPos()[0], 0])

        elif (direction == 'down'):
        	self.setPos([self.getPos()[0], self.getPos()[1] + self.speed])
        	# Ensures the lower edge does not pass off the bottom of the screen
        	if (self.getPos()[1] + self.getHeight() > SCREEN_HEIGHT):
        		self.setPos([self.getPos()[0], SCREEN_HEIGHT - self.getHeight()])

    # Returns true if self has collided with another ship
    def collide(self, other):
        # leftmost X value of self vs. rightmost value of other
        if (self.getPos()[0] >= other.getPos()[0] + other.getWidth()):
            return False
        # rightmost X value of self vs. leftmost value of other
        if (self.getPos()[0] + self.getWidth() <= other.getPos()[0]):
            return False
        # topmost Y value of self vs. bottommost value of other
        if (self.getPos()[1] <= other.getPos()[1] - other.getHeight()):
            return False
        # bottommost Y value of self vs. topmost value of other
        if (self.getPos()[1] - self.getHeight() >= other.getPos()[1]):
            return False
        return True

    def getPos(self):
        return self.pos

    def setPos(self, pos):
        self.pos = pos

    def getWidth(self):
        return self.width
    
    def getHeight(self):
        return self.height

    def getSpeed(self):
        return self.speed

    def setSpeed(self, newSpeed):
        self.speed = newSpeed

    def draw(self):
        # blits itself on the sceen
        SCREEN.blit(self.img, self.pos)

class Alpha(Ship):
    def __init__(self, pos, width, height, speed, rate):
        Ship.__init__(self, pos, 24, 20, 8, 5)
        self.sprite_rect = (324,356,24,20)
        self.img = SPRITE_SHEET.image_at(self.sprite_rect)

class Bravo(Ship):
    def __init__(self, pos, width, height, speed, rate):
        Ship.__init__(self, pos, 26, 20, 8, 5)
        self.sprite_rect = (324,308,26,20)
        self.img = SPRITE_SHEET.image_at(self.sprite_rect)

class Charlie(Ship):
    def __init__(self, pos, width, height, speed, rate):
        Ship.__init__(self,pos, 30, 32, 8, 5)
        self.sprite_rect = (322,254,30,32)
        self.img = SPRITE_SHEET.image_at(self.sprite_rect)

class Delta(Ship):
    def __init__(self, pos, width, height, speed, rate):
        Ship.__init__(self, pos, 30, 32, 8, 5)
        self.sprite_rect = (322,206,30,32)
        self.img = SPRITE_SHEET.image_at(self.sprite_rect)

class Enemy(Ship):
    def __init__(self, pos, width, height, speed, rate):
        Ship.__init__(self, pos, 30, 32, 12, 5)
        self.sprite_rect = (320,110,30,32)
        self.img = SPRITE_SHEET.image_at(self.sprite_rect)
